{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Requirement already satisfied: itchat in e:\\anaconda3\\lib\\site-packages\n",
      "Requirement already satisfied: pyqrcode in e:\\anaconda3\\lib\\site-packages (from itchat)\n",
      "Requirement already satisfied: pypng in e:\\anaconda3\\lib\\site-packages (from itchat)\n",
      "Requirement already satisfied: requests in e:\\anaconda3\\lib\\site-packages (from itchat)\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "You are using pip version 9.0.3, however version 18.0 is available.\n",
      "You should consider upgrading via the 'python -m pip install --upgrade pip' command.\n"
     ]
    }
   ],
   "source": [
    "!pip install itchat -i https://pypi.douban.com/simple"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**http://www.tuling123.com/**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "█\r"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Getting uuid of QR code.\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "通道\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Downloading QR code.\n",
      "Please scan the QR code to log in.\n",
      "Log in time out, reloading QR code.\n",
      "Getting uuid of QR code.\n",
      "Downloading QR code.\n",
      "Please scan the QR code to log in.\n",
      "Log in time out, reloading QR code.\n",
      "Getting uuid of QR code.\n",
      "Downloading QR code.\n",
      "Please scan the QR code to log in.\n"
     ]
    }
   ],
   "source": [
    "import itchat\n",
    "import requests\n",
    "import json\n",
    "import random\n",
    "APIKEY=\"****请输入你的APIKEY****\"\n",
    "def get_response(msg):\n",
    "    apiUrl = 'http://www.tuling123.com/openapi/api'\n",
    "    data = {\n",
    "        'key': APIKEY,\n",
    "        'info': msg,\n",
    "        'userid': 'wechat-robot',\n",
    "    }\n",
    "    r=requests.post(apiUrl,data=data)\n",
    "    {\"text\":\"****\"}.get(\"text\")\n",
    "    print(r.json().get(\"text\"))\n",
    "    return None \n",
    "get_response(\"channel\")\n",
    "#获得图灵机器人的智能回复\n",
    "def get_response(msg):\n",
    "    apiUrl = 'http://www.tuling123.com/openapi/api'\n",
    "    data = {\n",
    "        'key': APIKEY,\n",
    "        'info': msg,\n",
    "        'userid': 'wechat-robot',\n",
    "    }\n",
    "    try:\n",
    "        r = requests.post(apiUrl, data=data).json()\n",
    "        return r.get('text')\n",
    "    except:\n",
    "        return None\n",
    "#回复文字\n",
    "@itchat.msg_register(itchat.content.TEXT)\n",
    "def tuling_reply(msg):\n",
    "    reply = get_response(msg['Text'])\n",
    "    return reply\n",
    "itchat.auto_login(False,enableCmdQR=False)\n",
    "itchat.run()\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 使用itchat完成微信客服\n",
    "# 见文件夹wechat_auto"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
